<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";



class CAsk extends IPlugin 
{
    var $color;
    
    function CAsk($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "green";
    }
    
    function getPluginTag() { return "ask_expert"; }
    function getPluginName() { return "Information Plugin ask_expert"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuTitle", "MenuBlockTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Information"; } break;
            case "MenuTitle" : { return "Ask Expert"; } break;
        };
        return $result;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        $core = & getCoreInstance();
        $db = $core->getProperty("db");
        // debug($_GET,"get");  debug($_POST,"post");
        if ($_GET[action]=="doSubmit_") 
        {
            $is_active=0; if (isset($_POST[active])) $is_active=1; 
            $db->Exec("update ask_expert set is_active='$is_active', question='".str2db($_POST[question])."', answer='".str2db($_POST[answer])."' where ask_expert_id=$_GET[id]");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (ask_expert) : item (id=\"$_GET[id]\") was successfully submited</font><br>";
        }
        if ($_GET[action]=="doAdd_")  
        {
            $id=$db->Exec("insert into ask_expert (date) values(now())");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (ask expert) : item (id=\"$id\") was successfully added</font><br>"; 
        }
        if ($_GET[action]=="doDelete_")  
        {
            $db->Exec("delete from ask_expert where ask_expert_id=$_GET[id]");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (ask expert) : item (id=\"$_GET[id]\") was successfully removed</font><br>";
        }
        return true;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";
        
        switch ($_G["page"]) {
            default:
                $page = $this->renderask_expertList($render);
                break;
        }
        return $render->fetch("file:".$this->tplfolder."/".$page);
    }

    // ----------------------------------------------------------------------------------
    // --- [ renderMailList ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function renderask_expertList(& $render) 
    {
        $core = & getCoreInstance();
        $db = $core->getProperty("db");
        $pg = & $core->getSnippet("paginator");
        $tmp_url = $this->getURL()."&page=edit&tab=5&id=$uid&parent_id=".$curent_catalog_id;
        $page = $ppg = -1; /// curent count page
            $params = array ("cb_class" => & $this, "cb_func" => pg_ask_expert, "urlbase" => $tmp_url, "caption" => "Ask Expert", "emptymsg" => "No Ask Expert items collected yet", "header" => array ("id", "Question", "Answer", "Date", "Active", "Action"), "items" => "", "source" => "sql", // -- sql, array
        "sql_items" => "select ask_expert_id, question, answer, date, is_active from ask_expert order by date desc", "sql_count" => "select count(*) from ask_expert", "ippg" => 8, // -- items per page
        "nppv" => 5, // -- number pages per view
           "page" => & $page, "ppg" => & $ppg,);
        $HTML = $pg->Render($params);
        $render->assign("ask_expert", $HTML);
        //-----------------------------------------------
        if (isset($GLOBALS[result]))
        {
            $render->assign("result", $GLOBALS[result]);
            unset($GLOBALS[result]);
        }
                return "ask_expert.tpl.php";     
    }
         function pg_ask_expert($data) {
            
            
            //-----------------------------------------------
            $st_paginator_page = $_REQUEST['st_paginator_page'];
            $st_paginator_ppv = $_REQUEST['st_paginator_ppv'];
            $catalog_id=$data[catalog_id];
            $ask_expert_id=$data[ask_expert_id];
            $dt = array (
                array ("<form name=\"form".$data["ask_expert_id"]."\" action=\"?path=faq,ask_expert&action=doSubmit_&id={$catalog_id}&id=$ask_expert_id&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\" method=\"POST\" >".$data["ask_expert_id"], "align='center' "),
                array ("<textarea name=\"question\" wrap=\"virtual\" class=\"input_text\" style=\"height:53px;width:255px;\">".$data["question"]."</textarea>", ""),
                array ("<textarea name=\"answer\" wrap=\"virtual\" class=\"input_text\" style=\"height:53px;width:255px;\">".$data["answer"]."</textarea>", ""),
                array ($data["date"], "align='center' width=\"61px\""),
                array ("<input type='checkbox' name='active' ".($data["is_active"]==1 ? "checked" : "")." /> </form>", "align='center' "),
                array ("<a style=\"cursor:hand\" title=\"click for submit this line\" onclick=\"document.forms['form".$data["ask_expert_id"]."'].submit();\">apply</a>&nbsp;<a href=\"?path=faq,ask_expert&action=doDelete_&id=$ask_expert_id&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\" title=\"click for delete this line\">delete</a>", "align=\"center\"")  
                
                        );
            return $dt;
        }
    
    }    
    ?>